﻿#region Using Directives
using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TicketManager.Data;
using TicketManager.Entities;
using System.Data.SqlClient;
#endregion

namespace TicketManager.Website
{
	public partial class _Default : System.Web.UI.Page 
	{
		
        int ra;
        /// <summary>
        /// Handles the Load event of the Page class.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!IsPostBack)
            {
                BindDdlRap();
                BindDdlPhim();
                BindRptPhimDangChieu("aDangChieu");
            }
        }

        #region Bind data to DropDownList - Rap
        protected void BindDdlRap()
        {
            DataTable dt = DataRepository.RapProvider.GetAll().ToDataSet(false).Tables[0];
            if (dt!=null)
            {
                ddlRap.DataSource = dt;
                ddlRap.DataTextField = RapColumn.TenRap.ToString();
                ddlRap.DataValueField = RapColumn.RapId.ToString();
                ddlRap.DataBind();
                ddlRap.Items.Insert(0, "------Chọn rạp------");
            }
        }
        #endregion

        #region Bind data to DropDownList - Phim
        protected void BindDdlPhim()
        {
            //DataTable dt = DataRepository.PhimProvider.GetAll().ToDataSet(false).Tables[0];
            PhimFilters pfiter = new PhimFilters();            
            pfiter.Append(PhimColumn.TrangThai, "true");
            DataTable dt = DataRepository.PhimProvider.GetPaged(pfiter.ToString(), PhimColumn.TenPhim.ToString() + " ASC", 0, int.MaxValue, out ra).ToDataSet(false).Tables[0];

            if (dt != null)
            {
                ddlPhim.DataSource = dt;
                ddlPhim.DataTextField = PhimColumn.TenPhim.ToString();
                ddlPhim.DataValueField = PhimColumn.PhimId.ToString();
                ddlPhim.DataBind();
                ddlPhim.Items.Insert(0, "-----Chọn phim-----");
            }
        }
        #endregion

        #region Binding data to rptPhim
        protected void BindRptPhimDangChieu(string type)
        {
            DataTable dt = new DataTable();
            PhimFilters pFilter = new PhimFilters();

            switch (type)
            {
                case "aDangChieu":
                        pFilter.AppendLessThanOrEqual(PhimColumn.LichChieuBatDau, DateTime.Now.ToString());
                        pFilter.AppendGreaterThanOrEqual(PhimColumn.LichChieuKetThuc, DateTime.Now.ToString());
                        break;
                case "aSapChieu":
                        pFilter.AppendGreaterThanOrEqual(PhimColumn.LichChieuBatDau, DateTime.Now.ToString());
                        pFilter.AppendGreaterThanOrEqual(PhimColumn.LichChieuKetThuc, DateTime.Now.ToString());
                        break;
                default:
                    break;
            }
            pFilter.Append(PhimColumn.TrangThai, "true");

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TicketManager.Data.ConnectionString"].ConnectionString);
            
            SqlDataAdapter adapter;
            string sql = "SELECT top 4 [PhimID],[TenPhim],[LichChieuBatDau],[MoTa],[TrangThai],[LichChieuKetThuc],[AnhMoTa] FROM [dbo].[phim] WHERE ";
            sql += pFilter.ToString();
            sql += " ORDER BY TenPhim ASC";
            SqlCommand commd = new SqlCommand(sql, conn);
            adapter = new SqlDataAdapter(commd);
            adapter.Fill(dt);            
            if (dt.Rows.Count>0)
            {
                rptPhim.DataSource = dt;
                rptPhim.DataBind();
            }
        }
        protected void rptPhim_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;                
                HtmlImage imgAnh = (HtmlImage)e.Item.FindControl("imgAnh");
                HtmlAnchor aLink = (HtmlAnchor)e.Item.FindControl("aLink");
                HtmlAnchor aImg = (HtmlAnchor)e.Item.FindControl("aImg");
                HtmlAnchor aDatVe = (HtmlAnchor)e.Item.FindControl("aDatVe");
                imgAnh.Src = Convert.ToString(drv[PhimColumn.AnhMoTa.ToString()]);
                aLink.HRef = "~/BookingTickets.aspx";
                aLink.Title = Convert.ToString(drv[PhimColumn.TenPhim.ToString()]);
                aImg.HRef = aLink.HRef;
                aImg.Title = aLink.Title;
                aDatVe.HRef = aLink.HRef;
                aDatVe.Title = aLink.Title;
            }
        }
        #endregion

        #region OnClick event for LinkButton
        protected void linkButton_Click(object sender, EventArgs e)
        {
            LinkButton linkButton = (LinkButton)sender;
            switch (linkButton.ID)
            {
                case "aDangChieu" :
                    BindRptPhimDangChieu(linkButton.ID);
                    break;
                case "aSapChieu":
                    BindRptPhimDangChieu(linkButton.ID);
                    break;
                case "aBanVeSom":
                    BindRptPhimDangChieu(linkButton.ID);
                    break;
                //case "aTinTuc":                    
                default:
                    break;
            }
        }       
        #endregion
    }
}
